package com.bocai.dahuatech.ui.activity;

import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.NewsListBean;
import com.bocai.dahuatech.Bean.PopBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.utils.LoginDialogUtil;
import com.bocai.dahuatech.utils.SharePopUtil;
import com.bocai.dahuatech.utils.SharePrefencesUtil;
import com.bocai.dahuatech.utils.ShareUtil;
import com.bocai.dahuatech.utils.UrlData;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnClickListener;
import com.orhanobut.dialogplus.ViewHolder;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 咨询详情
 */
public class NewsDetailActivity extends BaseActivity{

    NewsListBean.ContentEntity bean;
    @Bind(R.id.web_newsdetail)
    WebView webNewsDetail;
    SharePopUtil sharePopUtil;
    ShareUtil shareUtil=new ShareUtil();
    String url="";
    int what;
    @Override
    protected int contentView() {
        return R.layout.activity_news_detail;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.news);
        bean= (NewsListBean.ContentEntity) getIntent().getSerializableExtra("bean");
        url=getIntent().getStringExtra("newsurl");
        what=getIntent().getIntExtra("what",0);
        if(what==1){

            mToolbar.inflateMenu(R.menu.menu_share);
        }
        initEvent();
        loadData();
    }

    private void loadData() {
        final ProgressBar bar = (ProgressBar)findViewById(R.id.my_profile_tracker);
        if(what==0){
            webNewsDetail.loadUrl(url);
        }else{
            webNewsDetail.loadUrl(UrlData.BASEURL+"home/NewsInfo?id=" + bean.getId());
        }
        webNewsDetail.getSettings().setJavaScriptEnabled(true);
        webNewsDetail.getSettings().setAllowFileAccess(true);
        webNewsDetail.getSettings().setDomStorageEnabled(true);
        webNewsDetail.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    bar.setVisibility(View.GONE);
                } else {
                    if (View.INVISIBLE == bar.getVisibility()) {
                        bar.setVisibility(View.VISIBLE);
                    }
                    bar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        webNewsDetail.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webNewsDetail.loadUrl(url);
                return true;
            }
        });
    }
    @Override
    protected void initEvent() {
        super.initEvent();
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                ArrayList<PopBean> list = new ArrayList<>();
                for (int i = 0; i < 2; i++) {
                    PopBean bean = new PopBean();
                    if (i == 0) {

                        bean.setId("1");
                        bean.setName(getResources().getString(R.string.collection));
                    }
                    else if (i == 1) {
                        bean.setId("2");
                        bean.setName(getResources().getString(R.string.share));
                    }
                    list.add(bean);
                }
                sharePopUtil = SharePopUtil.getInstance(NewsDetailActivity.this, mToolbar, 200, 100, list);
                sharePopUtil.setListener(new SharePopUtil.OnDatePopClickListener() {
                    @Override
                    public void onClick(SharePopUtil pu, PopBean date) {
                        if (date.getName().equals(getResources().getString(R.string.collection))) {
                            if (SharePrefencesUtil.getFAccessFlag(NewsDetailActivity.this) == 0) {
                                LoginDialogUtil.loginDialog(NewsDetailActivity.this);

                            } else {

                                ifCollected();
                            }
                            sharePopUtil.dismiss();
                        } else if (date.getName().equals(getResources().getString(R.string.share))) {
                            DialogPlus dialogPlus = DialogPlus.newDialog(NewsDetailActivity.this)
                                    .setContentHolder(new ViewHolder(R.layout.dialog_share))
                                    .setOnClickListener(new OnClickListener() {
                                        @Override
                                        public void onClick(DialogPlus dialog, View view) {
                                            switch (view.getId()) {
                                                case R.id.ll_dialog_qq:
                                                    shareUtil.showShare(NewsDetailActivity.this, "qq", bean.getImageUrl(),
                                                            bean.getTitle(), bean.getTitle(),UrlData.BASEURL+"home/NewsInfo?id=" + bean.getId());
                                                    break;
                                                case R.id.ll_dialog_weixin:
                                                    shareUtil.showShare(NewsDetailActivity.this, "weixin", bean.getImageUrl(),
                                                            bean.getTitle(), bean.getTitle(), UrlData.BASEURL+"home/NewsInfo?id=" + bean.getId());
                                                    break;
                                                case R.id.ll_dialog_weixinfriend:
                                                    shareUtil.showShare(NewsDetailActivity.this, "weixinfriend", bean.getImageUrl(),
                                                            bean.getTitle(), bean.getTitle(),UrlData.BASEURL+"home/NewsInfo?id=" + bean.getId());
                                                    break;
                                                case R.id.ll_dialog_sinaweibo:
                                                    shareUtil.showShare(NewsDetailActivity.this, "sinaweibo", bean.getImageUrl(),
                                                            bean.getTitle(), bean.getTitle(), UrlData.BASEURL+"home/NewsInfo?id=" + bean.getId());
                                                    break;
                                                case R.id.ll_dialog_qqzone:
                                                    shareUtil.showShare(NewsDetailActivity.this, "qqzone", bean.getImageUrl(),
                                                            bean.getTitle(), bean.getTitle(), UrlData.BASEURL+"home/NewsInfo?id=" + bean.getId());
                                                    break;

                                            }
                                            dialog.dismiss();
                                        }
                                    })
                                    .setExpanded(true)
                                    .setGravity(Gravity.BOTTOM)
                                    .create();
                            dialogPlus.show();
                            sharePopUtil.dismiss();
                        }
                    }
                });
                sharePopUtil.show();
//
                return false;
            }
        });
    }
    /**
     * 判断是否收藏
     */
    private void ifCollected() {
        getTestApi().ifNewsCollection(bean.getId(),2,SharePrefencesUtil.getFID(this))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(ifCollectedObserver);
    }

    Observer<BaseBean> ifCollectedObserver=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            Log.e("error",e.toString());
            hideLoading();
            showToast(R.string.neterror);
        }

        @Override
        public void onNext(BaseBean baseBean) {
            if(baseBean.getReturnInfo().equals(getResources().getString(R.string.nocollection))){
                //未收藏的话添加收藏，已收藏的话toast给用户已收藏
                addCollection();
            }else
            {
                showToast(R.string.newscollection);
            }
        }
    };


    /**
     * 添加收藏
     */
    private void addCollection() {
        getTestApi().addCollection(bean.getId(),"",2, SharePrefencesUtil.getFID(this))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(addCollectionObserver);
    }
    Observer<BaseBean> addCollectionObserver=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error",e.toString());

        }

        @Override
        public void onNext(BaseBean baseBean) {
            Log.e("info",baseBean.getReturnInfo());
            showToast(baseBean.getReturnInfo());
        }
    };

}
